import {
  createStore
} from "vuex";

export default createStore({
  state: {
    isCollapse: false,  // 左侧tab收缩
    userInfo: JSON.parse(localStorage.getItem('dtUsersVo') || '{}'),  // 左侧tab收缩
    visitedTags: JSON.parse(sessionStorage.getItem('visitedTags') || '[]'), // 导航条缓存窗口
    autoFindMenu: JSON.parse(localStorage.getItem('autoFindMenu') || '[]'), // 权限
  },
  getters: {},
  mutations: {
    // 设置用户个人信息
    setUserInfo(state, data){
      state.userInfo = data
      localStorage.setItem('dtUsersVo', JSON.stringify(data))
    },
    // 设置左侧tab收缩
    setIsCollapse(state, data){
      state.isCollapse = data
    },
    // 退出登录清空路由
    clearVisitedTags(state, data){
      state.visitedTags = []
      sessionStorage.setItem('visitedTags', '[]')
    },
    // 添加路由窗口
    setAddTags(state, payload) {
      // console.log('addTagsaddTagsaddTags', payload.route.meta.title);
      let flag = state.visitedTags.some(
        item => item.path === payload.route.path
      ); //打开标签后，判断数组中是否已经存在该路由
      if (!flag) {
        state.visitedTags.push(
          Object.assign({}, {
            path: payload.route.path,
            name: payload.route.name,
            title: payload.route.meta.title,
            params: payload.route.params,
            query:  payload.route.query,
            fullPath:  payload.route.fullPath,
          })
        );
        sessionStorage.setItem('visitedTags', JSON.stringify(state.visitedTags))
      } //数组中路由存在不push ,单击左侧路由变化,点击标签路由变化均触发
    },

    // 存储权限左侧功能栏
    setAutoFindMenu(state, data) {
      state.autoFindMenu = data
      localStorage.setItem('autoFindMenu', JSON.stringify(data))
    },

  },
  actions: {
    closeTags(state, payload) {
      let stas = state.state
      for (const [key, item] of stas.visitedTags.entries()) {
        if (item.path === payload.path) {
          stas.visitedTags.splice(key, 1);
          sessionStorage.setItem('visitedTags', JSON.stringify(stas.visitedTags))
          break;
        }
      }
    }
  },
  modules: {},
});